package com.inplaceedit.shared.webservice.impl;

import java.util.ArrayList;
import java.util.List;

import com.google.gwt.core.client.Scheduler;
import com.google.gwt.core.client.Scheduler.ScheduledCommand;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.inplaceedit.shared.dto.ContactDto;
import com.inplaceedit.shared.dto.NoteDto;
import com.inplaceedit.shared.webservice.ContactRemoteServiceAsync;
import com.inplaceedit.shared.webservice.impl.data.Contact;
import com.inplaceedit.shared.webservice.impl.data.Data;
import com.inplaceedit.shared.webservice.impl.data.Note;

public class ContactRemoteServiceImpl implements ContactRemoteServiceAsync {
	@Override
	public void deleteNote(final NoteDto object,
			final AsyncCallback<NoteDto> callback) {
		Scheduler.get().scheduleDeferred(new ScheduledCommand() {
			@Override
			public void execute() {
				try {
					callback.onSuccess(Converter.convert(Data.get().deleteNote(
							Converter.convert(object))));
				} catch (final Throwable e) {
					callback.onFailure(e);
				}
			}
		});
	}

	@Override
	public void get(final String id, final AsyncCallback<ContactDto> callback) {
		Scheduler.get().scheduleDeferred(new ScheduledCommand() {
			@Override
			public void execute() {
				try {
					callback.onSuccess(Converter.convert(Data.get().getContact(
							id)));
				} catch (final Throwable e) {
					callback.onFailure(e);
				}
			}
		});
	}

	@Override
	public void list(final AsyncCallback<List<ContactDto>> callback) {
		Scheduler.get().scheduleDeferred(new ScheduledCommand() {
			@Override
			public void execute() {
				try {
					callback.onSuccess(convertContactList(Data.get()
							.getContactList()));
				} catch (final Throwable e) {
					callback.onFailure(e);
				}
			}
		});
	}

	@Override
	public void listNotes(final String contactId,
			final AsyncCallback<List<NoteDto>> callback) {
		Scheduler.get().scheduleDeferred(new ScheduledCommand() {
			@Override
			public void execute() {
				try {
					callback.onSuccess(convertNoteList(Data.get().getNoteList(
							contactId)));
				} catch (final Throwable e) {
					callback.onFailure(e);
				}
			}
		});
	}

	@Override
	public void save(final ContactDto object,
			final AsyncCallback<ContactDto> callback) {
		Scheduler.get().scheduleDeferred(new ScheduledCommand() {
			@Override
			public void execute() {
				try {
					callback.onSuccess(Converter.convert(Data.get()
							.saveContact(Converter.convert(object))));
				} catch (final Throwable e) {
					callback.onFailure(e);
				}
			}
		});
	}

	@Override
	public void saveNote(final NoteDto object,
			final AsyncCallback<NoteDto> callback) {
		Scheduler.get().scheduleDeferred(new ScheduledCommand() {
			@Override
			public void execute() {
				try {
					callback.onSuccess(Converter.convert(Data.get().saveNote(
							Converter.convert(object))));
				} catch (final Throwable e) {
					callback.onFailure(e);
				}
			}
		});
	}

	private List<ContactDto> convertContactList(final List<Contact> from) {
		List<ContactDto> to = null;
		if (from != null) {
			to = new ArrayList<ContactDto>();
			for (final Contact item : from) {
				to.add(Converter.convert(item));
			}
		}
		return to;
	}

	private List<NoteDto> convertNoteList(final List<Note> from) {
		List<NoteDto> to = null;
		if (from != null) {
			to = new ArrayList<NoteDto>();
			for (final Note item : from) {
				to.add(Converter.convert(item));
			}
		}
		return to;
	}
}
